<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/16
 * Time: 17:38
 */

class bargain_paidControl extends SystemControl {
    private $links = array(
        array('url'=>'act=bargain_order&op=index','lang'=>'nc_manage')
    );

    public function __construct()
    {
        parent::__construct();
        Language::read('bargain_paid');
    }


    public  function indexOp()
    {


        $bargain_paid_model = Model('bargain_paid');
        $table = 'bargain_paid,member,bargain_order';
        $on = 'member.member_id=bargain_paid.member_id,bargain_order.bg_order_id=bargain_paid.bargain_order_id';
        $field = array(


            'bargain_paid.paid_size',
            'bargain_paid.add_time',
            'member.nickname',
            'bargain_order.bg_order_id',
            'bargain_order.bg_order_sn',
            'bargain_order.bg_title',
            'bargain_order.bg_order_price',
            'bargain_order.type',



         );
        //查询条件
        $condition = array();
        $condition['bargain_paid.add_time'] = array('neq','null');
        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['bargain_order.bg_order_sn'] = $_GET['search_sn'];
        }
        $time_from = strtotime($_GET['search_stime']);
        $time_to = strtotime($_GET['search_etime']);
        if($time_from && $time_to){
            $condition['bargain_paid.add_time'] = array('between',array($time_from,$time_to));
        }elseif($time_from){
            $condition['bargain_paid.add_time'] = array('egt',$time_from);
        }elseif($time_to){
            $condition['bargain_paid.add_time'] = array('elt',$time_to);
        }
        $bargain_bargain_list = $bargain_paid_model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->order('paid_id desc')
            ->page(30)
            ->select();
        Tpl::output('search',$_GET);
        Tpl::output('bargain_paid',$bargain_bargain_list);
        Tpl::output('page', $bargain_paid_model->showpage());
        Tpl::output('top_link',$this->sublink($this->links,'index'));
        Tpl::showpage('bargain_paid.index');
    }

    /**
     * 导出代付记录
     */

    public function exportOp()
    {

        $bargain_paid_model = Model('bargain_paid');
        $table = 'bargain_paid,member,bargain_order';
        $on = 'member.member_id=bargain_paid.member_id,bargain_order.bg_order_id=bargain_paid.bargain_order_id';
        $field = array(


            'bargain_paid.paid_size',
            'bargain_paid.add_time',
            'member.nickname',
            'bargain_order.bg_order_id',
            'bargain_order.bg_order_sn',
            'bargain_order.bg_title',
            'bargain_order.bg_order_price',
            'bargain_order.type',
       );
        //查询条件
        $condition = array();
        $condition['bargain_paid.add_time'] = array('neq','null');
        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['bargain_order.bg_order_sn'] = $_GET['search_sn'];
        }
        $time_from = strtotime($_GET['search_stime']);
        $time_to = strtotime($_GET['search_etime']);
        if($time_from && $time_to){
            $condition['bargain_paid.add_time'] = array('between',array($time_from,$time_to));
        }elseif($time_from){
            $condition['bargain_paid.add_time'] = array('egt',$time_from);
        }elseif($time_to){
            $condition['bargain_paid.add_time'] = array('elt',$time_to);
        }
        //每个EXCEL最大条数
        $exportSize = 1000;
        if(!is_numeric($_GET['curpage'])){
            $count = $bargain_paid_model->getBarginpaidcount($condition);
            $array = array();
            if ($count > $exportSize) {
                $page = ceil($count / $exportSize);
                for ($i=1; $i<=$page; $i++){
                    $limit1 = ($i -1 ) * $exportSize + 1;
                    $limit2 = $i * $exportSize > $count ? $count : $i * $exportSize;
                    $array[$i] = $limit1 . ' ~ ' . $limit2 ;
                }
                Tpl::output('list',$array);
                Tpl::showpage('export.excel');
            }else{
                $data = $bargain_paid_model
                    ->table($table)
                    ->field(implode(',',$field))
                    ->join('left,left')
                    ->on($on)
                    ->where($condition)
                    ->order('add_time desc')
                    ->limit($exportSize)
                    ->select();
                $this->createExcel($data);
            }
        }else{
            $limit1 = ($_GET['curpage'] - 1) * $exportSize;
            $limit2 = $exportSize;
            $data = $bargain_paid_model
                ->table($table)
                ->field(implode(',',$field))
                ->join('left,left')
                ->on($on)
                ->where($condition)
                ->order('add_time desc')
                ->limit("$limit1,$limit2")
                ->select();
            $this->createExcel($data);
        }

    }

    /**
     * 创建Excel
     * @param array $data
     *
     */

    public function createExcel($data)
    {
        import('libraries.excel');
        $excel_obj = new Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
        //表头
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_sn'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('member_name'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('paid_money'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('paid_time'));
        //数据
        foreach ((array)$data as $k=>$v){
            $tmp = array();
            $tmp[] = array('data'=>$v['bg_order_sn']);
            $tmp[] = array('data'=>$v['nickname']);
            $tmp[] = array('data'=>$v['paid_size']);
            $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['add_time']));

            //$tmp[] = array('format'=>'Number','data'=>ncPriceFormat($v['need_pay']));

            $excel_data[] = $tmp;

        }
        $excel_data = $excel_obj->charset($excel_data,CHARSET);
        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset('代付记录',CHARSET));
        $excel_obj->generateXML($excel_obj->charset('代付记录',CHARSET).$_GET['curpage'].'-'.date('Y-m-d-H',time()));

    }

}